package com.utils.jpa;


import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import java.lang.reflect.Field;

public class JpaUtils {

    public static CompoundSelection construct(Class resultClass, CriteriaBuilder cb, Root root) {
        int len = resultClass.getDeclaredFields().length;
        Selection[] selections = new Selection[len];

        for (int i = 0; i < len; i++) {
            Field filed = resultClass.getDeclaredFields()[i];
            selections[i] = root.get(filed.getName());
        }

        return cb.construct(resultClass, selections);
    }
}
